package com.example.jigsawpuzzles.slice;

import com.example.jigsawpuzzles.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.RadioContainer;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    RadioContainer radioContainer;
    int selectDiff = -1;
    private final int imgRequestCode = 1123;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 设置selectDiff结果为选择的图片编号
        radioContainer = (RadioContainer) findComponentById(ResourceTable.Id_radioContainer);
        radioContainer.setMarkChangedListener((radioContainer, i) -> {
            selectDiff = i;
        });

        // 选择图片按钮
        findComponentById(ResourceTable.Id_selectImg).setClickedListener(component -> {
            // 如果还未选择难度，则提示
            if (selectDiff == -1) {
                makeToast("请先选择游戏难度");
            } else {
                // 跳转到哪个页面
                Intent intentNext = new Intent();
                intentNext.setParam("selectDiff", selectDiff);
                // 包含了要跳转的页面信息
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("") // 跳转到哪个页面上，如果一个没有内容的字符串，则表示跳转本机
                        .withBundleName("com.example.jigsawpuzzles") // 跳转到哪个应用上
                        .withAbilityName("com.example.jigsawpuzzles.SelectImage") // 要跳转的页面
                        .build();

                // 把打包后的operation设置到意图中
                intentNext.setOperation(operation);

                // 跳转页面
                startAbility(intentNext);
            }


        });

        // 上传图片按钮
        findComponentById(ResourceTable.Id_uploadImg).setClickedListener(component -> {
            // 如果还未选择难度，则提示
            if (selectDiff == -1) {
                makeToast("请先选择游戏难度");
            } else {
                selectPic();
            }
        });
    }

    // 实现相册选图
    private void selectPic() {
        Intent intent = new Intent();

        // 调用android.intent.action.GET_CONTENT接口访问系统相册
        Operation opt = new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();

        intent.setOperation(opt);
        intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
        intent.setType("image/*");

        startAbilityForResult(intent, imgRequestCode);
    }

    // 实现相册选图的回调结果，并传递给下一个页面
    @Override
    protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
        if (requestCode == imgRequestCode) {
            //选择的Img对应的Uri
            String chooseImgUri = resultData.getUriString();

            // 设置参数，初始化intent
            Intent intentNext = new Intent();
            intentNext.setParam("selectImg", -1);   // -1代表自行上传图片
            intentNext.setParam("pixelMap", chooseImgUri);  // 图片url
            intentNext.setParam("selectDiff", selectDiff); //游戏难度

            //包含了要跳转的页面信息
            Operation operation = new Intent.OperationBuilder()
                    .withDeviceId("")//跳转到哪个页面上，如果一个没有内容的字符串，则表示跳转本机
                    .withBundleName("com.example.jigsawpuzzles")//跳转到哪个应用上
                    .withAbilityName("com.example.jigsawpuzzles.jigsaw")//要跳转的页面
                    .build();

            //把打包后的operation设置到意图中
            intentNext.setOperation(operation);

            //跳转页面
            startAbility(intentNext);

        }
    }

    void makeToast(String msg) {
        ToastDialog td = new ToastDialog(this);
        td.setText(msg);
        td.setAlignment(LayoutAlignment.CENTER);
        td.setDuration(1000);
        td.show();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
